package com.cloud.class04.controller;

import com.cloud.class04.entities.Pay;
import com.cloud.class04.entities.PayDTO;
import com.cloud.class04.resp.ResultData;
import com.cloud.class04.service.PayService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassDescription:
 * @JdkVersion: 17
 * @Author: yxy
 * @Created: 2024/4/7 17:30
 */
@RestController
@Tag(name = "支付微服务模块", description = "支付接口crud")
public class PayController {
    @Resource
    PayService payService;

    @PostMapping(value = "/pay/add")
    @Operation(summary = "新增", description = "新增支付流水方法,json串做参数")
    public ResultData<String> addPay(@RequestBody Pay pay) {
        System.out.println(pay.toString());
        int i = payService.add(pay);
        return ResultData.success("成功插入记录，返回值：" + i);

    }

    @DeleteMapping(value = "/pay/del/{id}")
    @Operation(summary = "删除", description = "删除支付流水方法")
    public ResultData<String> deletePay(@PathVariable("id") Integer id) {
        int i = payService.delete(id);
        return ResultData.success("成功删除记录，返回值：" + i);
    }

    @PutMapping(value = "/pay/update")
    @Operation(summary = "修改", description = "修改支付流水方法")
    public ResultData<String> updatePay(@RequestBody PayDTO payDTO) {
        Pay pay = new Pay();
        BeanUtils.copyProperties(payDTO, pay);
        int i = payService.update(pay);
        return ResultData.success("成功修改记录，返回值：" + i);

    }

    @GetMapping(value = "/pay/get/{id}")
    @Operation(summary = "按照ID查流水", description = "查询支付流水方法")
    public ResultData<Pay> getById(@PathVariable("id") Integer id) {
//        try {
//            int in = 1 / 0;
//        }catch (Exception e){
//            throw new RuntimeException("除数不能为0");
//        }
//
        Pay pay = payService.getById(id);
        return ResultData.success(pay);

    }

    //全部查询getall作为家庭作业
    @GetMapping(value = "/pay/getall")
    @Operation(summary = "查流水信息", description = "查询支付全部流水方法")
    public ResultData<List<Pay>> getAll() {
        return ResultData.success(payService.getAll());
    }

    @Value("${server.port}")
    private String port;

//    @GetMapping(value = "/pay/get/info")
//    public String getInfoByConsul(@Value("${class04.info}") String class04Info) {
//        return "class04Info:" + class04Info + "\t\t" + "  port:" + port;
//    }


    @GetMapping(value = "/pay/get/info")
    private String getInfoByConsul(@Value("${atclass.info}") String atguiguInfo)
    {
        return "atguiguInfo: "+atguiguInfo+"\t"+"port: "+port;
    }
}
